package org.mozhu.mboot.admin.config;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.mozhu.mboot.admin.AppUtils;
import org.mozhu.mboot.admin.system.resource.entity.Menu;
import org.mozhu.mboot.admin.system.resource.service.MenuService;
import org.mozhu.mboot.core.web.RequestContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ModelAttribute;

import java.util.List;

@org.springframework.web.bind.annotation.ControllerAdvice
@Slf4j
public class ControllerAdvice {
    @Autowired
    MenuService menuService;

    @ModelAttribute("menus")
    public List<Menu> menus() {
        String servletPath = RequestContextHolder.getRequest().getServletPath();
        log.info("menus: {}", servletPath);

        Long currentUserId = AppUtils.getCurrentUserId();
        if (currentUserId == null) {
            return Lists.newArrayList();

        }
        List<Menu> menus = menuService.getAvailableMenus(currentUserId);
        return menus;
    }

}
